<?php
class Products_IndexController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 18,
									'pageRange' => 4,
								  ); 
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';
							 
		$this->_paginat['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace); 
		//$ssFilter->unsetAll();
		$this->_arrParam['ssFilter']['lang'] 		= $this->_arrParam['lang'];	
		if(!empty($ssFilter ->keywords)) $this->_arrParam['ssFilter']['keywords'] = $ssFilter ->keywords;
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		/*$this->_langObj->setLangFile(array('default.language.tmx'));		
		Zend_Registry::set('Zend_Translate', $this->_langObj->generate());*/
		
		$info = new Zendvn_System_Info();
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam 			= $this->_arrParam;
		$this->view->currentController 	= $this->_currentController;
		$this->view->actionMain 		= $this->_actionMain;		
		$template_path 					= TEMPLATE_PATH . "/public/system";
		$this->loadTemplate($template_path,'template.ini','template');		
	}	
	public function indexAction(){
	   $tblMenu				= new Zendvn_Models_Menus();
	   $menu				= $tblMenu->listItem($this->_arrParam,array('task'=>'products-home'));
	   $this->view->Title 	= $menu['name'];
	   if(empty($this->view->Title)) $this->view->Title = 'Thực đơn - Nhà hàng Kim Hải';	   
	   $description 		= $menu['description_html'];
	   $keywords	 		= $menu['keywords_html'];
	   
	   $this->view->headTitle($this->view->Title,true);
	   if(!empty($description)) $this->view->headMeta(true)->setName('description',$description);	   
	   if(!empty($keywords)) $this->view->headMeta(true)->setName('keywords',$keywords);

	   $tblProduct 						= new Zendvn_Models_Products();
	   $this->view->Items   			= $tblProduct->listItem($this->_arrParam, Array('task'=>'product-list'));
	   $session 						= new Zend_Session_Namespace("default-index");
	   $this->view->count_item_current 	= count($this->view->Items);
	   $session->count_item_current 	= $this->view->count_item_current;
	   $this->view->Count   			= $tblProduct->countItem($this->_arrParam, null);
	}	
	public function filterAction(){
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'timkiem' && $this->_arrParam['task']=="filter" ){	
			$ssFilter->keywords = trim($this->_arrParam['keywords']);
		}	
		if($this->_arrParam['type'] == 'timkiem' && $this->_arrParam['task']=='clear'){
			unset($ssFilter->keywords);
		}
		
		$this->_redirect($this->_actionMain);
	}
	public function categoryAction(){
		$tblProduct = new Zendvn_Models_ProItem();
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-category'));
		
		$tblcat = new Zendvn_Models_Menus();
		$menu = $tblcat->listItem($this->_arrParam,array('task'=>'find-category'));
		$this->view->Category = str_replace("\\"," ",$menu['name']);
		$this->view->Title = str_replace("\\"," ",$menu['name']);
		if(!empty($menu['title_seo'])) $this->view->Title = $menu['title_seo'];
		$this->view->headTitle($this->view->Title,true);
		if(!empty($menu['description_html'])){
			$description = str_replace("\\"," ",$menu['description_html']);
			$this->view->headMeta(true)->setName('description',$description);
		}
		if(!empty($menu['keywords_html'])){
			$keywords	 = str_replace("\\"," ",$menu['keywords_html']);
			$this->view->headMeta(true)->setName('keywords',$keywords);
		}
		
		
		$this->view->total = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));		
		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));
		$paginator = new Zendvn_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
	}	
	public function detailAction(){	
		if(isset($this->_arrParam["id"]) && $this->_arrParam["id"] >0){	
			$tblDetail = new Zendvn_Models_ProItem();
			$this->view->Item 	= $tblDetail->getItem($this->_arrParam,array('task'=>'public-detail'));
			$this->view->Title = str_replace("\\","",$this->view->Item['name']);
			if (!empty($this->view->Item['title_seo'])) $this->view->Title = str_replace("\\"," ",$this->view->Item['title_seo']);
			$this->_arrParam['Item'] = $this->view->Item;
			$tblHits = $tblDetail->saveItem($this->_arrParam,array('task'=>'public-detail'));
			$description = str_replace("\\","",$this->view->Item['description_html']);
			$keywords	 = str_replace("\\","",$this->view->Item['keywords_html']);
			$this->view->headMeta(true)->setName('description',$description);
			$this->view->headMeta(true)->setName('keywords',$keywords);
			$this->view->headTitle($this->view->Title,true);
		}else{
			$this->_redirect($this->_actionMain);
		}
		
	}
	public function viewCartAction(){
		$yourCart = new Zend_Session_Namespace('default-index');	
		$items = (array)$yourCart->shoppingcart;	
		if($this->_request->isPost())
			foreach ($items as $key => $val){	
				if((int)$this->_arrParam[$key] > 0) $yourCart->shoppingcart->$key  = $this->_arrParam[$key];
				if((int)$this->_arrParam[$key] == 0) unset($yourCart->shoppingcart->$key);
			}
		$this->view->Items 		= (array)$yourCart->shoppingcart ;
	}
	public function addItemAction(){				
		$yourCart = new Zend_Session_Namespace('cart');		
		$ssInfo = $yourCart->getIterator();		
		$filter = new Zend_Filter_Digits();
		$id = $filter->filter($this->_arrParam['id']);			
		if(count($yourCart->cart) == 0){			
			$cart[$id] = 1;
			$yourCart->cart = $cart;
		}else{
			$tmp = $ssInfo['cart'];
			if(array_key_exists($id,$tmp) == true){
				$tmp[$id] = $tmp[$id] + 1;
			}else{
				$tmp[$id] = 1;
			}			
			$yourCart->cart = $tmp;			
		}
		$this->_helper->viewRenderer->setNoRender();
		$this->_redirect($this->_currentController . '/view-cart');
	}
	public function orderAction(){
		$yourCart = new Zend_Session_Namespace('default-index');			
		$ssInfo = (array) $yourCart->shoppingcart;	
		if(count($ssInfo) <= 0){
			$this->_redirect($this->_actionMain);
		}	
	}
	public function deleteAction(){
		$id = $this->_arrParam['id'];
		$yourCart = new Zend_Session_Namespace('cart');
		$cart = $yourCart->cart;
		unset($cart[$id]);
		$yourCart->cart = $cart;
		$this->_redirect($this->_currentController . '/view-cart');	
	}
	public function noteAction(){
		
	}
	public function errorCartAction(){
		
	}
	
	public function commentAction(){
		$tblComments = new Zendvn_Models_CommentsProducts();
		$captcha = new Zendvn_Captcha_Image();
		//9.Truy gia tri cua CAPTCHA ra VIEW
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();		
		if($this->_request->isPost()){
			$validator = new Products_Form_ValidateComment($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				$arrParam = $validator->getData(array('upload'=>true));
				$tblComments->saveItem($arrParam,array('task'=>'add-comment'));
				//$this->_redirect($this->_currentController . '/success');
			}
			$captcha->removeImg($captchaID);
		}
		$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
		$this->_helper->layout->disableLayout();
		
		
	}
	public function successAction(){
		$this->_helper->layout->disableLayout();
	}
	
}